﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using MpSoft.Library.BUSS.Customers;
using MpSoft.Library.DTO;

namespace MpSoft.Web.AdminCP.Controls.Customer
{
    public partial class PollAnswerControl : System.Web.UI.UserControl
    {
        #region BindControl

        private void BindControl()
        {
            if (PollID > 0)
            {
                var list = PollAnswerBUSS.PollAnswer_List(-1, PollID);
                grvDetail.DataSource = list;
                grvDetail.DataBind();
            }
        }

        #endregion

        #region Event

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["itemid"] != null)
            {
                int value;
                int.TryParse(Request.QueryString["itemid"].ToString(), out value);
                PollID = value;
            }
            if (!IsPostBack)
                BindControl();
        }

        protected void grvDetail_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            grvDetail.EditIndex = -1;
            BindControl();
        }

        protected void grvDetail_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.Equals("save") || e.CommandName.Equals("Insert"))
            {
                var txtName = grvDetail.FooterRow.FindControl("txtName") as TextBox;
                var txtDisplayOrder = grvDetail.FooterRow.FindControl("txtDisplayOrder") as TextBox;
                if (txtDisplayOrder != null)
                {
                    var obj = new PollAnswer();
                    int value;
                    int.TryParse(txtDisplayOrder.Text, out value);
                    obj.Name = txtName.Text;
                    obj.DisplayOrder = value;
                    obj.PollID = PollID;
                    if (PollAnswerBUSS.PollAnswer_Add(obj) > 0)
                        BindControl();
                }
                   
            }
        }

        protected void grvDetail_RowDataBound(object sender, GridViewRowEventArgs e)
        {

        }

        protected void grvDetail_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int gvid = 0;
            int.TryParse(grvDetail.DataKeys[e.RowIndex].Value.ToString(), out gvid);
            if (PollAnswerBUSS.PollAnswer_Delete(gvid) > 0)
                BindControl();
        }

        protected void grvDetail_RowEditing(object sender, GridViewEditEventArgs e)
        {
            grvDetail.EditIndex = e.NewEditIndex;
            BindControl();
        }

        protected void grvDetail_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            int gvid = 0;

            int.TryParse(grvDetail.DataKeys[e.RowIndex].Value.ToString(), out gvid);
            var txtName = grvDetail.Rows[e.RowIndex].FindControl("txtName") as TextBox;
            var txtDisplayOrder = grvDetail.Rows[e.RowIndex].FindControl("txtDisplayOrder") as TextBox;
            if (gvid > 0 && txtDisplayOrder != null)
            {
                var obj = new PollAnswer();
                int value;
                int.TryParse(txtDisplayOrder.Text, out value);
                obj.PollAnswerID = gvid;
                obj.Name = txtName.Text;
                obj.DisplayOrder = value;
                obj.PollID = PollID;
                if (PollAnswerBUSS.PollAnswer_Update(obj) > 0)
                {
                    grvDetail.EditIndex = -1;
                    BindControl();
                }
            }
        }

        protected void grvDetail_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.EmptyDataRow)
            {
                var txtName = e.Row.FindControl("txtName") as TextBox;
                var txtDisplayOrder = e.Row.FindControl("txtDisplayOrder") as TextBox;
                if (txtName != null)
                {
                    var obj = new PollAnswer();
                    int value;
                    int.TryParse(txtDisplayOrder.Text, out value);
                    obj.Name = txtName.Text;
                    obj.DisplayOrder = value;
                    obj.PollID = PollID;
                    if (PollAnswerBUSS.PollAnswer_Add(obj) > 0)
                        BindControl();
                }
            }
        }
        #endregion

        #region Properties

        public int PollID
        { set; get; }

        #endregion
    }
}